package com.read.controller;

import com.github.pagehelper.PageInfo;
import com.read.common.response.RespResult;
import com.read.dto.params.NovelNameParamsDto;
import com.read.dto.result.NovelNameResultDto;
import com.read.dto.result.NovelSectionResultDto;
import com.read.service.NovelService;
import com.read.util.RespUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author sh-sunb6
 * @description
 * @date 2019-02-19 17:37
 */
@Api(tags = "小说相关 Controller")
@RestController
@RequestMapping("/novel")
public class NovelController {

    private static final Logger logger = LoggerFactory.getLogger(NovelController.class);

    @Autowired
    private NovelService novelService;

    /**
     * @description 根据条件查询小说
     * @author sh-sunb6
     * @date 2019-02-20 17:20
     */
    @ApiOperation(value = "查询小说 - 分页", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "novelType", value = "小说类型(与typeChannel结合使用)", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "typeChannel", value = "小说类型归属(1男生频道，2女生频道，3二次元，9其他)", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name="novelName", value="小说名,模糊匹配", required=false, paramType="query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name="authorName", value="作者名", required=false, paramType="query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name="isOver", value="是否完本：1未完本，2完本，0未知", required=false, paramType="query", dataType = "int", defaultValue = ""),
            @ApiImplicitParam(name="pageNum", value="页码", required=true, paramType="query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name="pageSize", value="每页大小", required=true, paramType="query", dataType = "int", defaultValue = "10")
    })
    @GetMapping("/list")
    public RespResult selectNovel(NovelNameParamsDto paramsDto) {

        List<NovelNameResultDto> novelList = novelService.selectNovelByParams(paramsDto);
        return RespUtil.success(new PageInfo<>(novelList));
    }

    /**
     * @description 根据条件查询小说
     * @author sh-sunb6
     * @date 2019-02-20 17:20
     */
    @ApiOperation(value = "根据novelId查询小说详细信息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name="novelId", value="小说id", required=true, paramType="query", dataType = "int", defaultValue = "")
    })
    @GetMapping("/one")
    public RespResult selectNovelById(int novelId) {
        NovelNameResultDto novelInfo = novelService.selectNovelInfoByNovelId(novelId);
        int sectionId = novelService.selectFirstSectionIdByNovelId(novelId);

        novelInfo.setSectionId(sectionId);
        novelInfo.setSort(1);

        return RespUtil.success(novelInfo);
    }

    /**
     * @description 根据小说id查询章节信息
     * @author sh-sunb6
     * @date 2019-02-20 17:21
     */
    @ApiOperation(value = "根据小说id查询章节目录", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "novelId", value = "小说id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name="pageNum", value="页码", required=true, paramType="query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name="pageSize", value="每页大小", required=true, paramType="query", dataType = "int", defaultValue = "10")
    })
    @GetMapping("section")
    public RespResult selectNovelSection(@RequestParam int novelId, @RequestParam int pageNum, @RequestParam int pageSize) {

        List<NovelSectionResultDto> sectionList = novelService.selectNovelSectionByNovelId(novelId, pageNum, pageSize);
        return RespUtil.success(new PageInfo<>(sectionList));
    }

    /**
     * @description 根据章节id查询小说正文
     * @author sh-sunb6
     * @date 2019-02-20 17:50
     */
    @ApiOperation(value = "根据章节id查询小说正文", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "章节id", required = true, paramType = "query", dataType = "int")
    })
    @GetMapping("content")
    public RespResult selectNovelContent(@RequestParam int novelId, @RequestParam int sectionId, @RequestParam int sort) {
        return RespUtil.success(novelService.selectNovelContentBySectionId(novelId, sectionId, sort));
    }

    /**
     * @description 根据小说channel获取小说类型
     * @author sh-sunb6
     * @date 2019-02-20 17:50
     */
    @ApiOperation(value = "根据小说channel获取小说类型", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "channel", value = "小说频道，类型归属：1男生频道，2女生频道，3二次元，9其他", required = false, paramType = "query", dataType = "int")
    })
    @GetMapping("type")
    public RespResult selectNovelType(@RequestParam int channel) {
        return RespUtil.success(novelService.getNovelTypeByChannel(channel));
    }

}
